﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using publicLibrary;

namespace asMeishi.web.Controllers
{
    using Models;
    [authorityFilter(actionEnum.manage, actionTypeEnum.str)]
    public class levelController : Controller
    {
        DataSession<MongoDataContext, level> db = new DataSession<MongoDataContext, level>();
        //
        // GET: /level/

        public ActionResult Index()
        {
            return View(db.query.OrderBy(d => d.fraction));
        }

        [HttpPost]
        public ActionResult Index(FormCollection fc)
        {
            level level ;
            if (fc["Id"] == "")
            {
                level = new level();
            }
            else
            {
                level = db.GetById(fc["Id"]);
            }
            
            
            TryUpdateModel(level);
            level.access = 0;

            foreach (var item in fc["access"].trySplit(","))
            {
                level.access += item.ToInt() ?? 0;
            }
                        
            db.Save(level);
            return RedirectToAction("index");
        }
        public ActionResult del(string Id) {
            db.Delete(Id);
            return RedirectToAction("index");
        }

    }
}
